using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.VSPowerToys.PackInstaller
{
    public partial class TagListControl : UserControl
    {
        /// <summary>
        /// Raised when user selects a new tag from the list
        /// </summary>
        public event EventHandler<TagSelectedEventArgs> TagSelected;

        private SortedDictionary<string, TagItemControl> tagControls;
        private TagItemControl currentlySelectedTag;

        /// <summary>
        /// Property for which tag is currently selected in the list.
        /// </summary>
        public string CurrentlySelectedTag
        {
            get 
            {
                if(currentlySelectedTag != null)
                {
                    return currentlySelectedTag.TagName;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// Constructor, sets up the dictionary of tag text to tag control.
        /// </summary>
        public TagListControl()
        {
            tagControls = new SortedDictionary<string, TagItemControl>();
            InitializeComponent();
        }

        /// <summary>
        /// Gets a control created for each tag, and then adds those controls to the control array.  We do this
        /// as a two step process so we get the tags all sorted before they get added to the control array.
        /// </summary>
        public void AddTags(Collection<string> tags)
        {
            foreach (string tag in tags)
            {
                AddTagControl(tag);
            }

            int tabIndex = 0;
            foreach (KeyValuePair<string, TagItemControl> keyValuePair in tagControls)
            {
                this.Controls.Add(keyValuePair.Value);
                keyValuePair.Value.TabIndex = tabIndex;
                keyValuePair.Value.TabStop = false;
                keyValuePair.Value.BringToFront();
                tabIndex++;
            }
        }

        /// <summary>
        /// Clears out the controls from both the dictionary and the control array.
        /// </summary>
        public void ClearTags()
        {
            this.tagControls.Clear();
            this.Controls.Clear();
        }

        /// <summary>
        /// Unselects the current tag by setting it's state to none, and then clearing the reference.
        /// </summary>
        public void ClearTagSelection()
        {
            if (currentlySelectedTag != null)
            {
                currentlySelectedTag.CurrentTagState = TagItemControl.TagState.None;
                currentlySelectedTag = null;
            }
        }

        /// <summary>
        /// Adds a new tag if we don't have one for the string we're adding already.
        /// If we do have the tag in the list already then we increment its tag count.
        /// </summary>
        /// <param name="tag" />
        private void AddTagControl(string tag)
        {
            if (!tagControls.ContainsKey(tag))
            {
                TagItemControl newControl = new TagItemControl(tag);
                newControl.Name = tag + Strings.TagControlName;
                newControl.TagCount = 1;
                newControl.Dock = DockStyle.Top;
                newControl.TagSelected += new EventHandler<TagSelectedEventArgs>(tagControl_TagSelected);
                tagControls.Add(tag, newControl);
            }
            else
            {
                TagItemControl existingTagControl = tagControls[tag];
                existingTagControl.TagCount = existingTagControl.TagCount + 1;
            }
        }

        /// <summary>
        /// event raised when one of the tags gets selected.  First thing we have to do
        /// is unselect the current tag, then save which tag is now selected.
        /// And then propagate the message up.
        /// </summary>
        /// <param name="sender" />
        /// <param name="e" />
        private void tagControl_TagSelected(object sender, TagSelectedEventArgs e)
        {
            TagItemControl newTag = sender as TagItemControl;
            if (currentlySelectedTag != null && ((newTag != null) && (newTag != currentlySelectedTag)) )
            {
                currentlySelectedTag.CurrentTagState = TagItemControl.TagState.None;
            }

            currentlySelectedTag = newTag;

            if (TagSelected != null)
            {
                this.TagSelected(this, e);
            }
        }
    }
}
